//
//  LDTNetWorkRequestManager.m
//  EPA
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 浮夸. All rights reserved.
//

#import "LDTNetWorkRequestManager.h"

@interface LDTNetWorkRequestManager ()

//存储回调block的属性
//@property(nonatomic,copy)ResultBlock block;


@end

@implementation LDTNetWorkRequestManager

/////单例方法
//+ (instancetype)defaultManager{
//    static LDTNetWorkRequestManager *manager = nil;
//    @synchronized(self) {
//        if (!manager) {
//            manager = [[LDTNetWorkRequestManager alloc]init];
//        }
//    }
//    return manager;
//}


//请求方法
+ (void)requestURL:(NSURL *)url Method:(URLRequestMethod)method Params:(NSDictionary *)params Result:(ResultBlock)resultBlock
{
    ///空的可变字符串
    NSMutableString *urlString = [NSMutableString new];
    //isKindOfClass来确定一个对象是否是一个类的成员，或者是派生自该类的成员
    if ([url isKindOfClass:[NSURL class]]) {
        //拼接url
        [urlString appendString:url.absoluteString];
        
    }else if ([url isKindOfClass:[NSString class]]){
        
        [urlString appendString:(NSString *)url];
    }
    if (urlString.length ==0) {
        NSError *error = [NSError errorWithDomain:@"EPA" code:3.1415926 userInfo:@{@"reason":@"你输入的网址不正确"}];
        resultBlock(nil,nil,error);
        return;
    }
    //把参数字典转换为字符串
    LDTNetWorkRequestManager *manager = [LDTNetWorkRequestManager new];
    
    NSString *paraStr = [manager strFromDict:params];
    
    switch (method) {
        case LDT_GET:
        {
            if (paraStr.length > 0) {
                
                [urlString appendFormat:@"?%@",paraStr];
            }
            [manager requestURL:[NSURL URLWithString:urlString] ResultBlock:resultBlock];
            
        }
            break;
            
        case LDT_POST:
            [manager postURL:urlString paraStr:paraStr ResultBlock:resultBlock];
            break;
        default:
            break;
    }
    
}

#pragma mark - GET网络请求

- (void)requestURL:(NSURL *)url ResultBlock:(ResultBlock)block
{
    NSURLSessionDataTask *task = [[NSURLSession sharedSession]dataTaskWithURL:url completionHandler:block];
                                  
    //开始网络请求
    [task resume];
}

#pragma mark - POST网络请求

- (void)postURL:(NSString *)urlStr paraStr:(NSString *)paraStr ResultBlock:(ResultBlock)block
{
    //创建NSURL
    NSURL *url = [NSURL URLWithString:urlStr];
    //创建可变请求
    NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url];
    //设置POST方法
    [mRequest setHTTPMethod:@"POST"];
    //请求体
    [mRequest setHTTPBody:[paraStr dataUsingEncoding:NSUTF8StringEncoding]];
    
   
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]dataTaskWithRequest:mRequest completionHandler:block];
                                      
    [dataTask resume];
    
}




- (NSString *)strFromDict:(NSDictionary *)dict
{
    //空数组
    NSMutableArray *array = [NSMutableArray new];
    for (NSString *key in dict) {
        NSString *str = [NSString stringWithFormat:@"%@=%@",key,dict[key]];
        [array addObject:str];
    }
    //数组拼接字符串
    NSString *para = [array componentsJoinedByString:@"&"];
    NSLog(@"%@",para);
    //返回
    return para;
}




@end
